import {getCouponReceivePage, receiveCoupon} from "@src/api/coupon";
import pageHandleUtil from "@src/common/pageHandleUtil";
import router from "@src/router";
import color from "@src/common/color";

Component({
  properties: {
    receiveCode: {
      type: String
    }
  },
  data: {
    navHeight: 80,
    isSendValid: true,
    receiveStatus: true,
    couponPrototype: null,
    availablePeriods: null,
    initReceiveBtn: "",
    businessName: "",
    showUseTimeArr: []
  },
  methods: {
    onLogin() {
      this.receivePageInfoData();
    },
    /*按钮点击*/
    receiveBtn() {
      if (this.data.receiveStatus) {
        //true表示可以领取
        wx.showLoading({title: "领取中...", mask: true});
        this.receiveReq();
      }
    },
    receivePageInfoData() {
      let data = {
        receiveCode: this.data.receiveCode
      };
      return getCouponReceivePage(data).then(res => {
        if (res.code === 0) {
          //可以领取
          let businessName = res.data.info.businessName;
          let availablePeriods = res.data.info.availablePeriods;
          let couponPrototype = res.data.info.couponPrototype;
          let initReceiveBtn = res.data.msg;
          let showUseTimeArr = [];
          let receiveStatus = false;
          //可以领取
          if (res.data.code === 100) {
            receiveStatus = true;
          }

          //时段可使用渲染
          if (couponPrototype.hasTimeLimit && availablePeriods && availablePeriods.length > 0) {
            if (availablePeriods[0].timeType === "week") {
              showUseTimeArr = pageHandleUtil.dillCouponWeek(availablePeriods);
            } else {
              //day
              showUseTimeArr = pageHandleUtil.dillCouponDay(availablePeriods)
            }
          } else {
            showUseTimeArr.push({
              returnString: `全部时段可用`,
              timeString: ``
            });
          }
          if (couponPrototype.dateType === "DATE_TYPE_FIX _TIME_RANGE") {
            //时间范围
            showUseTimeArr.push({
              returnString: `券的有效时间`,
              timeString: `${couponPrototype.beginTime}-${couponPrototype.endTime}`
            });
          } else if (couponPrototype.dateType === "DATE_TYPE_FIX_TERM") {
            //固定时间
            if (couponPrototype.fixedBeginTerm === 0) {
              showUseTimeArr.push({returnString: `券领取后立即生效`, timeString: ``});
            } else if (couponPrototype.fixedBeginTerm === 1) {
              showUseTimeArr.push({returnString: `券领取后第二天开始生效`, timeString: ``});
            } else {
              showUseTimeArr.push({returnString: `券领取后第${couponPrototype.fixedBeginTerm + 1}天开始生效`, timeString: ``});
            }
          }
          //处理背景色
          this.bgRender(couponPrototype.color);
          this.setData({
            isSendValid: true, businessName,
            receiveStatus, showUseTimeArr, initReceiveBtn, couponPrototype, availablePeriods
          })
        } else {
          this.setData({isSendValid: false, noValidTitle: res.msg})
        }
      })
    },
    receiveReq() {
      //领取卡券接口
      let data = {
        receiveCode: this.data.receiveCode
      };
      receiveCoupon(data).then(res => {
        wx.hideLoading();
        if (res.code === 0) {
          if (res.succeed) {
            this.handleReceiveSuccess(res.data)
          }
        } else {
          wx.showToast({title: res.msg, icon: "none", duration: 5000});
          this.receivePageInfoData();
        }
      }).catch(res=> {
        wx.hideLoading();
      })
    },
    handleReceiveSuccess(couponCode) {
      //得到的券码
      wx.showModal({
        title: "提示", content: "领取成功", cancelText: "返回", confirmText: "去使用",
        success: (res) => {
          if (res.confirm) {
            router.push({name: 'member/couponDetail', params: {couponCode: couponCode}})
          } else if (res.cancel) {
            this.receivePageInfoData();
          }
        }
      });
    },
    //背景渲染
    bgRender(getColor) {
      //修改背景颜色
      let colorObj = color.getThemeObj(getColor);
      wx.setBackgroundColor({
        backgroundColorTop: colorObj.color, // 顶部窗口的背景色为白色
        backgroundColorBottom: colorObj.color // 底部窗口的背景色为白色
      });
    }
  }
})
